<?php

/**
 * This is the model class for table "artists".
 *
 * The followings are the available columns in table 'artists':
 * @property integer $id
 * @property string $fullname
 * @property string $nickname
 * @property string $brief
 * @property string $email
 * @property string $gender
 * @property string $image
 * @property string $imagecover
 * @property string $dateofbirth
 * @property string $status
 * @property string $createddate
 * @property string $url
 */
class Artists extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'artists';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nickname,brief', 'required'),
			array('email','email'),
			array('nickname','unique'),
			array('fullname, nickname, email, url', 'length', 'max'=>255),
			array('brief', 'length', 'max'=>1000),
			array('gender, status', 'length', 'max'=>1),
            array('image, imagecover', 'unsafe'),
            array('image, imagecover', 'file', 'types'=>'jpg, gif, png', 'on'=>'create'),
			array('image, imagecover', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, fullname, nickname, brief, email, gender, image, imagecover, dateofbirth, status, createddate, url', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			// Relation with Images
			'images'=>array(self::HAS_MANY, 'ArtistImages', 'artist_id'),
			// Relation with Profile
			'profile'=>array(self::HAS_ONE, 'ArtistProfile', 'artist_id'),
			// Relation with Social
			'socials'=>array(self::HAS_MANY, 'ArtistSocial', 'artist_id'),
            // Relation with SeoTools
			'seotool'=>array(self::HAS_ONE, 'SeoTools', 'table_id', 'condition'=>'tables="artists"'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'fullname' => 'Họ và tên',
			'nickname' => 'Nghệ danh',
			'brief' => 'Thông tin cơ bản',
			'email' => 'Email',
			'gender' => 'Giới tính',
			'image' => 'Image',
			'imagecover' => 'Imagecover',
			'dateofbirth' => 'Ngày sinh',
			'status' => 'Status',
			'createddate' => 'Createddate',
			'url' => 'Url',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('fullname',$this->fullname,true);
		$criteria->compare('nickname',$this->nickname,true);
		$criteria->compare('brief',$this->brief,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('gender',$this->gender,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('imagecover',$this->imagecover,true);
		$criteria->compare('dateofbirth',$this->dateofbirth,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('createddate',$this->createddate,true);
		$criteria->compare('url',$this->url,true);
		$criteria->order = 'id DESC';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Artists the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
